﻿using CommonLibrary.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;

namespace CommonLibrary.WorkFlow
{
    internal static class MailManager
    {
        private static string progressMail;
        private static string successMail;
        private static string faultMail;
        private static string ccMail;

        static MailManager()
        {
            progressMail = File.ReadAllText(HttpRuntime.AppDomainAppPath + @"\MailTemplate\progress.txt");
            successMail = File.ReadAllText(HttpRuntime.AppDomainAppPath + @"\MailTemplate\success.txt");
            faultMail = File.ReadAllText(HttpRuntime.AppDomainAppPath + @"\MailTemplate\fault.txt");
            ccMail = File.ReadAllText(HttpRuntime.AppDomainAppPath + @"\MailTemplate\cc.txt");
        }

        /// <summary>
        /// 发送审批过程中的提醒邮件。
        /// </summary>
        /// <param name="toUserId"></param>
        public static void SendProgressEmail(
            CommonDatabase db,
            int toUserId,
            int fromUserId,
            string flowName)
        {
            // 通知工作人员审批当前工作项
            try
            {
                var fromUser = db.sys_UserInfo
                                   .Where(k => k.UserId == fromUserId)
                                   .FirstOrDefault();
                if (fromUser == null)
                    return;

                var toUser = db.sys_UserInfo.Where(k => k.UserId == toUserId)
                                 .FirstOrDefault();
                if (toUser == null &&
                    toUser.WorkEmail != null &&
                    Regex.IsMatch(toUser.WorkEmail.Trim(), @"^\w+@xk-soft.com$"))
                    return;

                var message = string.Format(
                                progressMail,
                                toUser.UserName,
                                fromUser.UserName,
                                flowName,
                                DateTime.Now.ToString("yyyy/MM/dd hh:mm"));
                var email = toUser.WorkEmail;

                Task.Factory.StartNew(() =>
                {
                    var helper = new MailHelper();
                    helper.SendEmail(email, "信开审批系统提醒", message);
                });
            }
            catch
            {
            }
        }

        /// <summary>
        /// 发送审批全部通过的提醒邮件。
        /// </summary>
        public static void SendSuccessEmail(
            CommonDatabase db,
            int toUserId,
            string flowName)
        {
            try
            {
                var toUser = db.sys_UserInfo
                               .Where(k => k.UserId == toUserId)
                               .FirstOrDefault();
                if (toUser == null &&
                    toUser.WorkEmail != null &&
                    Regex.IsMatch(toUser.WorkEmail.Trim(), @"^\w+@xk-soft.com$"))
                    return;

                string message = string.Format(
                    successMail,
                    toUser.UserName,
                    flowName,
                    DateTime.Now.ToString("yyyy/MM/dd hh:mm"));

                var email = toUser.WorkEmail;

                Task.Factory.StartNew(() =>
                {
                    var helper = new MailHelper();
                    helper.SendEmail(email, "信开审批系统提醒", message);
                });

            }
            catch
            {
            }
        }

        /// <summary>
        /// 发送审批被拒绝的提醒邮件
        /// </summary>
        public static void SendFaultEmail(
            CommonDatabase db,
            int toUserId,
            string flowName)
        {
            try
            {
                var toUser = db.sys_UserInfo
                                   .Where(k => k.UserId == toUserId)
                                   .FirstOrDefault();
                if (toUser == null &&
                    toUser.WorkEmail != null &&
                    Regex.IsMatch(toUser.WorkEmail.Trim(), @"^\w+@xk-soft.com$"))
                    return;

                string message = string.Format(
                    faultMail,
                    toUser.UserName,
                    flowName,
                    DateTime.Now.ToString("yyyy/MM/dd hh:mm"));

                var email = toUser.WorkEmail;

                Task.Factory.StartNew(() =>
                {
                    var helper = new MailHelper();
                    helper.SendEmail(email, "信开审批系统提醒", message);
                });

            }
            catch
            {
            }
        }

        public static void SendCCEmail(
            CommonDatabase db,
            int toUserId,
            int fromUserId,
            string flowName)
        {
            // 通知工作人员审批当前工作项
            try
            {
                var fromUser = db.sys_UserInfo
                                   .Where(k => k.UserId == fromUserId)
                                   .FirstOrDefault();
                if (fromUser == null)
                    return;

                var toUser = db.sys_UserInfo.Where(k => k.UserId == toUserId)
                                 .FirstOrDefault();
                if (toUser == null &&
                    toUser.WorkEmail != null &&
                    Regex.IsMatch(toUser.WorkEmail.Trim(), @"^\w+@xk-soft.com$"))
                    return;

                var message = string.Format(
                                ccMail,
                                toUser.UserName,
                                fromUser.UserName,
                                flowName,
                                DateTime.Now.ToString("yyyy/MM/dd hh:mm"));
                var email = toUser.WorkEmail;

                Task.Factory.StartNew(() =>
                {
                    var helper = new MailHelper();
                    helper.SendEmail(email, "信开审批系统提醒", message);
                });
            }
            catch
            {
            }
        }
    }
}
